{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 167,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import numpy as np \n",
    "#模型评估\n",
    "from sklearn.linear_model import LinearRegression, RidgeCV, LassoCV\n",
    "from sklearn.model_selection import train_test_split\n",
    "\n",
    "\n",
    "from sklearn.metrics import mean_squared_error\n",
    "from sklearn.metrics import r2_score  #评价回归预测模型的性能\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 168,
   "metadata": {},
   "outputs": [],
   "source": [
    "train = pd.read_csv('FE_day.csv')   #加载数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 169,
   "metadata": {},
   "outputs": [],
   "source": [
    "#去掉离群数据\n",
    "cnt_data = train['cnt'] \n",
    "index = 0\n",
    "for index_value in cnt_data :\n",
    "    if index_value < 100 or index_value > 9000 : \n",
    "        train.drop(index, inplace=True)\n",
    "    index = index + 1   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 170,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>season_1</th>\n",
       "      <th>season_2</th>\n",
       "      <th>season_3</th>\n",
       "      <th>season_4</th>\n",
       "      <th>mnth_1</th>\n",
       "      <th>mnth_2</th>\n",
       "      <th>mnth_3</th>\n",
       "      <th>mnth_4</th>\n",
       "      <th>mnth_5</th>\n",
       "      <th>mnth_6</th>\n",
       "      <th>...</th>\n",
       "      <th>weekday_4</th>\n",
       "      <th>weekday_5</th>\n",
       "      <th>weekday_6</th>\n",
       "      <th>temp</th>\n",
       "      <th>atemp</th>\n",
       "      <th>hum</th>\n",
       "      <th>windspeed</th>\n",
       "      <th>holiday</th>\n",
       "      <th>workingday</th>\n",
       "      <th>yr</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0.355170</td>\n",
       "      <td>0.373517</td>\n",
       "      <td>0.828620</td>\n",
       "      <td>0.284606</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.379232</td>\n",
       "      <td>0.360541</td>\n",
       "      <td>0.715771</td>\n",
       "      <td>0.466215</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.171000</td>\n",
       "      <td>0.144830</td>\n",
       "      <td>0.449638</td>\n",
       "      <td>0.465740</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.175530</td>\n",
       "      <td>0.174649</td>\n",
       "      <td>0.607131</td>\n",
       "      <td>0.284297</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0.209120</td>\n",
       "      <td>0.197158</td>\n",
       "      <td>0.449313</td>\n",
       "      <td>0.339143</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 33 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "   season_1  season_2  season_3  season_4  mnth_1  mnth_2  mnth_3  mnth_4  \\\n",
       "0         1         0         0         0       1       0       0       0   \n",
       "1         1         0         0         0       1       0       0       0   \n",
       "2         1         0         0         0       1       0       0       0   \n",
       "3         1         0         0         0       1       0       0       0   \n",
       "4         1         0         0         0       1       0       0       0   \n",
       "\n",
       "   mnth_5  mnth_6 ...  weekday_4  weekday_5  weekday_6      temp     atemp  \\\n",
       "0       0       0 ...          0          0          1  0.355170  0.373517   \n",
       "1       0       0 ...          0          0          0  0.379232  0.360541   \n",
       "2       0       0 ...          0          0          0  0.171000  0.144830   \n",
       "3       0       0 ...          0          0          0  0.175530  0.174649   \n",
       "4       0       0 ...          0          0          0  0.209120  0.197158   \n",
       "\n",
       "        hum  windspeed  holiday  workingday  yr  \n",
       "0  0.828620   0.284606        0           0   0  \n",
       "1  0.715771   0.466215        0           0   0  \n",
       "2  0.449638   0.465740        0           1   0  \n",
       "3  0.607131   0.284297        0           1   0  \n",
       "4  0.449313   0.339143        0           1   0  \n",
       "\n",
       "[5 rows x 33 columns]"
      ]
     },
     "execution_count": 170,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#去掉与模型无关数据和分离y\n",
    "y_date =  train[\"cnt\"]\n",
    "x_date = train.drop('cnt',axis=1)\n",
    "x_date = x_date.drop('instant',axis=1)\n",
    "x_date.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 232,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(584, 33) (146, 33)\n"
     ]
    }
   ],
   "source": [
    "#分割测试数据和训练数据 80%训练数据 20%测试数据\n",
    "X_train, X_test, y_train, y_test = train_test_split(\n",
    "    x_date, y_date, test_size = 0.2, random_state = 0)\n",
    "print(X_train.shape, X_test.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 245,
   "metadata": {},
   "outputs": [],
   "source": [
    "#1* Linear Regression without regularization 最小二乘线性回归\n",
    "\n",
    "regr = LinearRegression()\n",
    "regr.fit(X_train,y_train)\n",
    "\n",
    "y_train_result = regr.predict(X_train)\n",
    "y_test_result = regr.predict(X_test)\n",
    "\n",
    "#regr.coef_\n",
    "#regr.intercept_\n",
    "rmse_test = np.sqrt(mean_squared_error(y_train,y_train_result))\n",
    "rmse_train = np.sqrt(mean_squared_error(y_test,y_test_result))\n",
    "\n",
    "R2_test_score = r2_score(y_test,y_test_result)\n",
    "R2_train_score = r2_score(y_train, y_train_result)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 200,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "alpha =  1.0\n"
     ]
    }
   ],
   "source": [
    "#岭回归模型测试\n",
    "#设置正则系数\n",
    "alpha = [0.1, 1, 10, 100]\n",
    "#初始化模型\n",
    "ridge = RidgeCV(alpha)\n",
    "#训练模型\n",
    "ridge.fit(X_train, y_train)\n",
    "#得到最佳系数\n",
    "alpha = ridge.alpha_\n",
    "\n",
    "print(\"alpha = \", alpha)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 241,
   "metadata": {},
   "outputs": [],
   "source": [
    "#使用训练好的模型进行预测\n",
    "y_train_ridge_result = ridge.predict(X_train)\n",
    "y_test_ridge_result = ridge.predict(X_test)\n",
    "\n",
    "rmse_test_ridge = np.sqrt(mean_squared_error(y_train,y_train_ridge_result))\n",
    "rmse_train_ridge = np.sqrt(mean_squared_error(y_test,y_test_ridge_result))\n",
    "R2_test_score_ridge = r2_score(y_test,y_test_ridge_result)\n",
    "R2_train_score_ridge = r2_score(y_train, y_train_ridge_result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 246,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "alphas =  2.895976824821181\n"
     ]
    }
   ],
   "source": [
    "##lasso模型测试\n",
    "\n",
    "lasso = LassoCV()\n",
    "lasso.fit(X_train,y_train)\n",
    "\n",
    "print(\"alphas = \", lasso.alpha_)\n",
    "\n",
    "#3. 模型性能\n",
    "mse_cv = np.mean(lasso.mse_path_, axis = 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 243,
   "metadata": {},
   "outputs": [],
   "source": [
    "#训练误差\n",
    "y_train_lasso_result = lasso.predict(X_train)\n",
    "rmse_train_lasso = np.sqrt(mean_squared_error(y_train,y_train_lasso_result))\n",
    "\n",
    "#测试误差\n",
    "y_test_lasso_result = lasso.predict(X_test)\n",
    "rmse_test_lasso = np.sqrt(mean_squared_error(y_test,y_test_lasso_result))\n",
    "\n",
    "r2_score_train = r2_score(y_train,y_train_lasso_result)\n",
    "r2_score_test_lasso = r2_score(y_test,y_test_lasso_result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 228,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>columns</th>\n",
       "      <th>coef_lr</th>\n",
       "      <th>coef_ridge</th>\n",
       "      <th>coef_lasso</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>weekday_5</td>\n",
       "      <td>1.483905e+16</td>\n",
       "      <td>90.501139</td>\n",
       "      <td>96.024731</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>23</th>\n",
       "      <td>weekday_4</td>\n",
       "      <td>1.483905e+16</td>\n",
       "      <td>51.084016</td>\n",
       "      <td>45.772308</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>22</th>\n",
       "      <td>weekday_3</td>\n",
       "      <td>1.483905e+16</td>\n",
       "      <td>-21.827277</td>\n",
       "      <td>-0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>20</th>\n",
       "      <td>weekday_1</td>\n",
       "      <td>1.483905e+16</td>\n",
       "      <td>-95.925845</td>\n",
       "      <td>-59.854565</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>21</th>\n",
       "      <td>weekday_2</td>\n",
       "      <td>1.483905e+16</td>\n",
       "      <td>-85.398708</td>\n",
       "      <td>-39.211748</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25</th>\n",
       "      <td>weekday_6</td>\n",
       "      <td>1.262747e+16</td>\n",
       "      <td>239.872522</td>\n",
       "      <td>98.147409</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>19</th>\n",
       "      <td>weekday_0</td>\n",
       "      <td>1.262747e+16</td>\n",
       "      <td>-178.305847</td>\n",
       "      <td>-276.718377</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>12</th>\n",
       "      <td>mnth_9</td>\n",
       "      <td>2.938893e+15</td>\n",
       "      <td>672.186249</td>\n",
       "      <td>633.142726</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>mnth_5</td>\n",
       "      <td>2.938893e+15</td>\n",
       "      <td>455.304598</td>\n",
       "      <td>363.483363</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>13</th>\n",
       "      <td>mnth_10</td>\n",
       "      <td>2.938893e+15</td>\n",
       "      <td>277.821332</td>\n",
       "      <td>420.331847</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>6</th>\n",
       "      <td>mnth_3</td>\n",
       "      <td>2.938893e+15</td>\n",
       "      <td>179.897159</td>\n",
       "      <td>269.969476</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>9</th>\n",
       "      <td>mnth_6</td>\n",
       "      <td>2.938893e+15</td>\n",
       "      <td>124.646272</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>mnth_8</td>\n",
       "      <td>2.938893e+15</td>\n",
       "      <td>145.317708</td>\n",
       "      <td>17.199927</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>7</th>\n",
       "      <td>mnth_4</td>\n",
       "      <td>2.938893e+15</td>\n",
       "      <td>-9.607334</td>\n",
       "      <td>-0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>mnth_2</td>\n",
       "      <td>2.938893e+15</td>\n",
       "      <td>-261.287858</td>\n",
       "      <td>-24.440631</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>mnth_1</td>\n",
       "      <td>2.938893e+15</td>\n",
       "      <td>-401.379964</td>\n",
       "      <td>-141.189992</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>10</th>\n",
       "      <td>mnth_7</td>\n",
       "      <td>2.938893e+15</td>\n",
       "      <td>-283.022581</td>\n",
       "      <td>-368.165219</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15</th>\n",
       "      <td>mnth_12</td>\n",
       "      <td>2.938893e+15</td>\n",
       "      <td>-411.709669</td>\n",
       "      <td>-149.032284</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>14</th>\n",
       "      <td>mnth_11</td>\n",
       "      <td>2.938893e+15</td>\n",
       "      <td>-488.165914</td>\n",
       "      <td>-230.891915</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>26</th>\n",
       "      <td>temp</td>\n",
       "      <td>2.512000e+03</td>\n",
       "      <td>1822.971783</td>\n",
       "      <td>2745.969188</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>yr</td>\n",
       "      <td>1.959125e+03</td>\n",
       "      <td>1969.662479</td>\n",
       "      <td>1960.249071</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>27</th>\n",
       "      <td>atemp</td>\n",
       "      <td>1.284000e+03</td>\n",
       "      <td>1640.056333</td>\n",
       "      <td>1061.189838</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>29</th>\n",
       "      <td>windspeed</td>\n",
       "      <td>-1.407500e+03</td>\n",
       "      <td>-1263.856313</td>\n",
       "      <td>-1271.771645</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>28</th>\n",
       "      <td>hum</td>\n",
       "      <td>-1.823000e+03</td>\n",
       "      <td>-1463.944073</td>\n",
       "      <td>-1509.230628</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>31</th>\n",
       "      <td>workingday</td>\n",
       "      <td>-2.211579e+15</td>\n",
       "      <td>159.024629</td>\n",
       "      <td>1.832529</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>30</th>\n",
       "      <td>holiday</td>\n",
       "      <td>-2.211579e+15</td>\n",
       "      <td>-220.591305</td>\n",
       "      <td>-314.331213</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>season_4</td>\n",
       "      <td>-3.688379e+16</td>\n",
       "      <td>669.782277</td>\n",
       "      <td>423.751677</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>season_2</td>\n",
       "      <td>-3.688379e+16</td>\n",
       "      <td>53.388452</td>\n",
       "      <td>-0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>season_3</td>\n",
       "      <td>-3.688379e+16</td>\n",
       "      <td>92.465653</td>\n",
       "      <td>0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>season_1</td>\n",
       "      <td>-3.688379e+16</td>\n",
       "      <td>-815.636381</td>\n",
       "      <td>-1010.277349</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>weathersit_1</td>\n",
       "      <td>-4.245898e+16</td>\n",
       "      <td>685.951884</td>\n",
       "      <td>411.315594</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>17</th>\n",
       "      <td>weathersit_2</td>\n",
       "      <td>-4.245898e+16</td>\n",
       "      <td>253.787761</td>\n",
       "      <td>-0.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>18</th>\n",
       "      <td>weathersit_3</td>\n",
       "      <td>-4.245898e+16</td>\n",
       "      <td>-939.739645</td>\n",
       "      <td>-1168.717807</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "         columns       coef_lr   coef_ridge   coef_lasso\n",
       "24     weekday_5  1.483905e+16    90.501139    96.024731\n",
       "23     weekday_4  1.483905e+16    51.084016    45.772308\n",
       "22     weekday_3  1.483905e+16   -21.827277    -0.000000\n",
       "20     weekday_1  1.483905e+16   -95.925845   -59.854565\n",
       "21     weekday_2  1.483905e+16   -85.398708   -39.211748\n",
       "25     weekday_6  1.262747e+16   239.872522    98.147409\n",
       "19     weekday_0  1.262747e+16  -178.305847  -276.718377\n",
       "12        mnth_9  2.938893e+15   672.186249   633.142726\n",
       "8         mnth_5  2.938893e+15   455.304598   363.483363\n",
       "13       mnth_10  2.938893e+15   277.821332   420.331847\n",
       "6         mnth_3  2.938893e+15   179.897159   269.969476\n",
       "9         mnth_6  2.938893e+15   124.646272     0.000000\n",
       "11        mnth_8  2.938893e+15   145.317708    17.199927\n",
       "7         mnth_4  2.938893e+15    -9.607334    -0.000000\n",
       "5         mnth_2  2.938893e+15  -261.287858   -24.440631\n",
       "4         mnth_1  2.938893e+15  -401.379964  -141.189992\n",
       "10        mnth_7  2.938893e+15  -283.022581  -368.165219\n",
       "15       mnth_12  2.938893e+15  -411.709669  -149.032284\n",
       "14       mnth_11  2.938893e+15  -488.165914  -230.891915\n",
       "26          temp  2.512000e+03  1822.971783  2745.969188\n",
       "32            yr  1.959125e+03  1969.662479  1960.249071\n",
       "27         atemp  1.284000e+03  1640.056333  1061.189838\n",
       "29     windspeed -1.407500e+03 -1263.856313 -1271.771645\n",
       "28           hum -1.823000e+03 -1463.944073 -1509.230628\n",
       "31    workingday -2.211579e+15   159.024629     1.832529\n",
       "30       holiday -2.211579e+15  -220.591305  -314.331213\n",
       "3       season_4 -3.688379e+16   669.782277   423.751677\n",
       "1       season_2 -3.688379e+16    53.388452    -0.000000\n",
       "2       season_3 -3.688379e+16    92.465653     0.000000\n",
       "0       season_1 -3.688379e+16  -815.636381 -1010.277349\n",
       "16  weathersit_1 -4.245898e+16   685.951884   411.315594\n",
       "17  weathersit_2 -4.245898e+16   253.787761    -0.000000\n",
       "18  weathersit_3 -4.245898e+16  -939.739645 -1168.717807"
      ]
     },
     "execution_count": 228,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#对三个模型的比较\n",
    "#对比各个特征系数的权重\n",
    "feat_names = X_train.columns\n",
    "compare_data_W = pd.DataFrame({\"columns\":list(feat_names), \"coef_lr\":list(regr.coef_), \"coef_ridge\":list( ridge.coef_)\n",
    "                            , \"coef_lasso\":list(lasso.coef_)})\n",
    "compare_data_W.sort_values(by = ['coef_lr'], ascending = False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 240,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>columns</th>\n",
       "      <th>RMSE</th>\n",
       "      <th>R2_score</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>LassoCV</td>\n",
       "      <td>800.553543</td>\n",
       "      <td>0.850979</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>LinearRegression</td>\n",
       "      <td>737.690895</td>\n",
       "      <td>0.849524</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>RidgeCV</td>\n",
       "      <td>736.005308</td>\n",
       "      <td>0.854180</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "            columns        RMSE  R2_score\n",
       "2           LassoCV  800.553543  0.850979\n",
       "0  LinearRegression  737.690895  0.849524\n",
       "1           RidgeCV  736.005308  0.854180"
      ]
     },
     "execution_count": 240,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#模型测试数据评价比较\n",
    "feat_names = [\"LinearRegression\", \"RidgeCV\", \"LassoCV\"]\n",
    "feat_rmst_test = [rmse_test, rmse_test_ridge, rmse_test_lasso]\n",
    "feat_r2_test = [R2_test_score, R2_test_score_ridge, r2_score_test_lasso]\n",
    "compare_data_y = pd.DataFrame({\"columns\":list(feat_names), \"RMSE\":feat_rmst_test, \"R2_score\":feat_r2_test})\n",
    "compare_data_y.sort_values(by = ['RMSE'], ascending = False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#三种模型的比较\n",
    "通过测试数据对比 岭回归和lasso模型的回归系数权重差不太多，都要比最小二乘小很多，但是lasso出现了很多0， 模型评价指标岭回归模型都要优与其他模型\n",
    "所以在这个数据中岭回归模型是最优的"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
